home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / 3DGPL 1.0 / CODE / CLIPPER / CLIPP-2D.C next >
Encoding:
C/C++ Source or Header  |  1997-03-08  |  8.5 KB  |  226 lines  |  [TEXT/MACA]

  1. /** 3DGPL **************************************************\
  2.  *  ()                                                     *
  3.  *  2D line and polygon clipping.                          *
  4.  *                                                         *
  5.  *  Defines:                                               *
  6.  *   C_line_x_clipping       Clipping a line horizontally; *
  7.  *   C_line_y_clipping       Clipping a line vertically;   *
  8.  *                                                         *
  9.  *   C_polygon_x_clipping    Horizontal polygon cliping.   *
  10.  *                                                         *
  11.  *  (6/1995) By Sergei Savchenko. (savs@cs.mcgill.ca).     *
  12.  *  Copyright (c) 1995 Sergei Savchenko.                   *
  13.  *  THIS SOURCE CODE CAN'T BE USED FOR COMERCIAL PURPOSES  *
  14.  *  WITHOUT AUTHORISATION                                  *
  15. \***********************************************************/
  16.  
  17. #ifdef __MWERKS__
  18. #include "hardware.h"           /* hardware specific stuff */
  19. #include "clipper.h"             /* 2D macros */
  20. #else
  21. #include "../hardware/hardware.h"           /* hardware specific stuff */
  22. #include "../clipper/clipper.h"             /* 2D macros */
  23. #endif
  24.  
  25. //#include "../hardware/hardware.h"           /* hardware specific stuff */
  26. //#include "../clipper/clipper.h"             /* 2D macros */
  27.  
  28. int C_2D_clipping;                          /* type of performed clipping */
  29.  
  30. /**********************************************************\
  31.  *  Line clipping using binary search technique.          *
  32.  *                                                        *
  33.  *  RETUNRNS: 0, when line is compleately ouside          *
  34.  *  --------- 1, boundaries otherwise.                    *
  35.  *                                                        *
  36.  *  SETS:  C_2D_clipping to 1 when both first or both     *
  37.  *  -----  points were changed, 0 otherwise.              *
  38. \**********************************************************/
  39.  
  40. int C_line_x_clipping(int **vertex1,int **vertex2,int dimension)
  41. {
  42.  register int i;
  43.  register int whereto;
  44.  register int *l,*r,*m,*t;                  /* left right and midle and tmp */
  45.  static int g_store0[C_MAX_DIMENSIONS];     /* static stores for clipped vxes */
  46.  static int g_store1[C_MAX_DIMENSIONS];
  47.  static int g_store2[C_MAX_DIMENSIONS];
  48.  static int g_store3[C_MAX_DIMENSIONS];
  49.  static int g_store4[C_MAX_DIMENSIONS];
  50.  static int g_store5[C_MAX_DIMENSIONS];
  51.  int **vmn,**vmx;                           /* so that *vmn[0] < *vmx[0] */
  52.  int swap;                                  /* were coordinates swaped? */
  53.  
  54.  C_2D_clipping=0;                           /* default no clipping yet */
  55.  
  56.  if((*vertex1)[0]<(*vertex2)[0])
  57.  { swap=0; vmn=vertex1; vmx=vertex2; }      /* so that *vmn[0] < *vmx[0] */
  58.  else
  59.  { swap=1; vmn=vertex2; vmx=vertex1; }
  60.  
  61.  if(((*vmn)[0]>C_X_CLIPPING_MAX)||((*vmx)[0]<C_X_CLIPPING_MIN)) return(0);
  62.  else
  63.  {
  64.   if((*vmn)[0]<=C_X_CLIPPING_MIN)           /* clipping */
  65.   {
  66.    HW_copy_int(*vmn,l=g_store0,dimension);  /* copying old vertices */
  67.    HW_copy_int(*vmx,m=g_store1,dimension);
  68.    r=g_store2;                              
  69.  
  70.    whereto=0;
  71.    while(m[0]!=C_X_CLIPPING_MIN)
  72.    {
  73.     if(whereto==1) { t=l; l=m; m=t; }
  74.     else           { t=r; r=m; m=t; }
  75.     for(i=0;i<dimension;i++) m[i]=(l[i]+r[i])>>1;
  76.     whereto=m[0]<C_X_CLIPPING_MIN;
  77.    }
  78.    *vmn=m;                                  /* that is why m[] is static */
  79.    C_2D_clipping=swap^1;
  80.   }
  81.  
  82.   if((*vmx)[0]>=C_X_CLIPPING_MAX)           /* clipping */
  83.   {
  84.    HW_copy_int(*vmn,l=g_store3,dimension);  /* copying old vertices */
  85.    HW_copy_int(*vmx,m=g_store4,dimension);
  86.    r=g_store5;                              
  87.  
  88.    whereto=0;
  89.    while(m[0]!=C_X_CLIPPING_MAX)
  90.    {
  91.     if(whereto==1) { t=l; l=m; m=t; }
  92.     else           { t=r; r=m; m=t; }
  93.     for(i=0;i<dimension;i++) m[i]=(l[i]+r[i])>>1;
  94.     whereto=m[0]<C_X_CLIPPING_MAX;
  95.    }
  96.    *vmx=m;                                  /* that is why m[] is static */
  97.    C_2D_clipping|=swap&1;
  98.   }
  99.  }
  100.  return(1);                                 /* partialy or not clipped */
  101. }
  102.  
  103. /**********************************************************\
  104.  *  Line clipping using binary search technique.          *
  105.  *                                                        *
  106.  *  RETUNRNS: 0, when line is compleately ouside          *
  107.  *  --------- 1, boundaries otherwise.                    *
  108.  *                                                        *
  109.  *  SETS:  C_2D_clipping to 1 when both first or both     *
  110.  *  -----  points were changed, 0 otherwise.              *
  111. \**********************************************************/
  112.  
  113. int C_line_y_clipping(int **vertex1,int **vertex2,int dimension)
  114. {
  115.  register int i;
  116.  register int whereto;
  117.  register int *l,*r,*m,*t;                  /* left right and midle and tmp */
  118.  static int g_store0[C_MAX_DIMENSIONS];     /* static stores for clipped vxes */
  119.  static int g_store1[C_MAX_DIMENSIONS];
  120.  static int g_store2[C_MAX_DIMENSIONS];
  121.  static int g_store3[C_MAX_DIMENSIONS];
  122.  static int g_store4[C_MAX_DIMENSIONS];
  123.  static int g_store5[C_MAX_DIMENSIONS];
  124.  int **vmn,**vmx;                           /* so that *vmn[1] < *vmx[1] */
  125.  int swap;                                  /* were coordinates swaped? */
  126.  
  127.  C_2D_clipping=0;                           /* default no clipping yet */
  128.  
  129.  if((*vertex1)[1]<(*vertex2)[1])
  130.  { swap=0; vmn=vertex1; vmx=vertex2; }      /* so that *vmn[1] < *vmx[1] */
  131.  else
  132.  { swap=1; vmn=vertex2; vmx=vertex1; }
  133.  
  134.  if(((*vmn)[1]>C_Y_CLIPPING_MAX)||((*vmx)[1]<C_Y_CLIPPING_MIN)) return(0);
  135.  else
  136.  {
  137.   if((*vmn)[1]<=C_Y_CLIPPING_MIN)           /* clipping */
  138.   {
  139.    HW_copy_int(*vmn,l=g_store0,dimension);  /* copying old vertices */
  140.    HW_copy_int(*vmx,m=g_store1,dimension);
  141.    r=g_store2;                             
  142.  
  143.    whereto=0;
  144.    while(m[1]!=C_Y_CLIPPING_MIN)
  145.    {
  146.     if(whereto==1) { t=l; l=m; m=t; }
  147.     else           { t=r; r=m; m=t; }
  148.     for(i=0;i<dimension;i++) m[i]=(l[i]+r[i])>>1;
  149.     whereto=m[1]<C_Y_CLIPPING_MIN;
  150.    }
  151.    *vmn=m;                                  /* that is why m[] is static */
  152.    C_2D_clipping=swap^1;
  153.   }
  154.  
  155.   if((*vmx)[1]>=C_Y_CLIPPING_MAX)           /* clipping */
  156.   {
  157.    HW_copy_int(*vmn,l=g_store3,dimension);  /* copying old vertices */
  158.    HW_copy_int(*vmx,m=g_store4,dimension);
  159.    r=g_store5;                             
  160.  
  161.    whereto=0;
  162.    while(m[1]!=C_Y_CLIPPING_MAX)
  163.    {
  164.     if(whereto==1) { t=l; l=m; m=t; }
  165.     else           { t=r; r=m; m=t; }
  166.     for(i=0;i<dimension;i++) m[i]=(l[i]+r[i])>>1;
  167.     whereto=m[1]<C_Y_CLIPPING_MAX;
  168.    }
  169.    *vmx=m;                                  /* that is why m[] is static */
  170.    C_2D_clipping|=swap&1;
  171.   }
  172.  }
  173.  return(1);                                 /* partialy or not clipped */
  174. }
  175.  
  176. /**********************************************************\
  177.  *  Creating a x-clipped polygon.                         *
  178.  *                                                        *
  179.  *  RETURNS: number of elements in the clipped element.   *
  180.  *  -------  (0 when compleately behind view plane)       *
  181.  *                                                        *
  182.  *          |    |        1-2-3-4-5-6-1  -> 2'-3'-5'-6'   *
  183.  *          |5'  |6'                                      *
  184.  *       5*-*----*-*6      If first dot in the line is    *
  185.  *       /  |    |   \     being clipped both points are  *
  186.  *     4*   |    |    *1   copyed If no clipping or second*
  187.  *       \  |    |   /     dot is clipped then only second*
  188.  *       3*-*----*-*2      dot is copyed. If both points  *
  189.  *          |3'  |2'       are clipped well, neither is   *
  190.  *                         copyed.                        *
  191. \**********************************************************/
  192.  
  193. int C_polygon_x_clipping(register int *from,register int *to,
  194.                          int dimension,int length
  195.                         )
  196. {
  197.  register int i;
  198.  int *v1,*v2,new_lng=0;
  199.  int *first_vrtx=to;                        /* begining of the source */
  200.  
  201.  for(i=0;i<length;i++)                      /* for all edges */
  202.  {
  203.   v1=from; from+=dimension; v2=from;        /* taking two vertices */
  204.  
  205.   if(C_line_x_clipping(&v1,&v2,dimension))  /* clipping */
  206.   {
  207.    if(C_2D_clipping)                        /* depends which one was clipped */
  208.    {
  209.     HW_copy_int(v1,to,dimension); to+=dimension;
  210.     HW_copy_int(v2,to,dimension); to+=dimension;  
  211.     new_lng+=2;                             /* first point clipped */
  212.    }
  213.    else
  214.    {
  215.     HW_copy_int(v2,to,dimension); to+=dimension; 
  216.     new_lng++;                              /* second point clipped */
  217.    }
  218.   }
  219.  }
  220.  HW_copy_int(first_vrtx,to,dimension);      /* looping the polygon vertices */
  221.  
  222.  return(new_lng);
  223. }
  224.  
  225. /**********************************************************/
  226.